package com.looeyWei;

import java.awt.*;
import java.util.ArrayList;

public class SimpleLayout extends LayoutAdapter {

    /**
     * 记录 panel 有哪些控件
     */
    ArrayList<Component> list =  new ArrayList<Component>();
    @Override
    public void addLayoutComponent(Component comp, Object constraints) {
        list.add(comp);
    }

    @Override
    public void removeLayoutComponent(Component comp) {
        list.remove(comp);
    }

    @Override
    public void layoutContainer(Container parent) {
        int width = parent.getWidth();
        int height = parent.getHeight();
        // 实现网格布局, 每个格子长宽100
        int x = 0;
        int y = 0;
        for (Component c : list) {
            c.setBounds(x, y, 100, 100);
            x += 100;
            if (x > width) {
                y += 100;
                x = 0;
            }
        }
    }
}
